﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManager.DataAccess.Model;

namespace HotelManager.WebUI.Models
{
    /// <summary>
    /// Save booking information
    /// </summary>
    public class BookingModel
    {
        [Required(ErrorMessage = "Firstname is required!")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Lastname is required!")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Address is required!")]
        public string Address { get; set; }

        [Required(ErrorMessage = "Email is required!")]
        [EmailAddressAttribute(ErrorMessage = "Invalid email format!")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Phone number is required!")]
        [PhoneNumberAttribute(ErrorMessage = "Phone number is a sequence of 10 digits (111-111-1111 or 1111111111)!")]
        public string PhoneNumber { get; set; }

        [Required(ErrorMessage = "Number of rooms is required!")]
        public int NumberOfRoom { get; set; }

        public int RoomTypeId { get; set; }

        public string SpecialRequirement { get; set; }

        public BookingRoomInfo RoomInfo { get; set; }

    }

    #region "Attributes to validate"

    /// <summary>
    /// Register an attribute to validate email
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class EmailAddressAttribute : RegularExpressionAttribute
    {
        private const string pattern = @"^\w+([-+.]*[\w-]+)*@(\w+([-.]?\w+)){1,}\.\w{2,4}$";

        static EmailAddressAttribute()
        {
            // necessary to enable client side validation
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAddressAttribute), typeof(RegularExpressionAttributeAdapter));
        }

        public EmailAddressAttribute()
            : base(pattern)
        {
        }
    }

    /// <summary>
    /// Register an attribute to validate email
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class PhoneNumberAttribute : RegularExpressionAttribute
    {
        private const string pattern = @"^\(?\d{3}\)?[\s-]?\d{3}\-?\d{4}$";

        static PhoneNumberAttribute()
        {
            // necessary to enable client side validation
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(PhoneNumberAttribute), typeof(RegularExpressionAttributeAdapter));
        }

        public PhoneNumberAttribute()
            : base(pattern)
        {
        }
    }

    #endregion

}